package test2;

import java.util.Scanner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class test2_3 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String[] id = new String[100];
		for(int i=0;i<n;i++){
			id[i] = sc.next();
		}
		ArrayList strList = new ArrayList(); 
		while(true){
			//int k=0;
			
			//String[] cho = new String[100];
			String ch = sc.next();
			while(ch.equals("sort1")||ch.equals("sort2")){
				strList.add(ch);
				ch = sc.next();
			}
			//int r = 0;
			for(int r=0;r<strList.size();r++){
				/*if(!cho[r].equals("sort1")&&!cho[r].equals("sort2")){
					
					break;
				}*/
					
				switch((String)strList.get(r)){
				case "sort1":
					String[] year = new String[n];
					String[] month = new String[n];
					String[] day = new String[n];
					String[] data = new String[n];
					for(int i=0;i<n;i++){
					year[i] = id[i].substring(6, 10);
					month[i] = id[i].substring(10, 12);
					day[i] = id[i].substring(12, 14);
					data[i] = year[i]+"-"+month[i]+"-"+day[i];
					}
					Arrays.sort(data);
					for(int i=0;i<n;i++){
						System.out.println(data[i]);
					}
					
					
				    break;
				case "sort2":
					String[] data2 = new String[n];
					for(int i=0;i<n;i++){
						data2[i] = id[i].substring(6, 14);
						
					}
					for(int i=0;i<n;i++){
						for(int j=0;j<n-1;j++){
							String pre = data2[j];
							int pre1=Integer.parseInt(pre);
							String next = data2[j+1];
							int next1=Integer.parseInt(next);
							if(pre1>next1){
								String temp = data2[j];
								data2[j] = data2[j+1];
								data2[j+1] = temp;
								String temp2 = id[j];
								id[j] = id[j+1];
								id[j+1] = temp2;
								
							}
						}
					}
					for(int i=0;i<n;i++){
						System.out.println(id[i]);
					}
					
					break;
				
				}
			}
			System.out.println("exit");
				
				
				
			
			
		}
		
			
			}
		}


